package com.polaris.live.viewmodel

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.net.userService
import com.polaris.live.resp.base.ListResp
import com.polaris.live.widget.refresh.OnListListener
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.translation.R

/**
 * BlackViewModel
 *
 * @author Created by 半仙 on 2023/11/2/002 15:55
 */
class BlackViewModel : BaseViewModel(), OnListListener {

    val blackLiveData = MutableLiveData<ResultState<ListResp<UserBasicVo>>>()

    val pageSize = 20  // 每页记录数

    private fun getBlack(pageNumber: Int, pageSize: Int = this.pageSize) {
        request({ userService.getBlackList(pageNumber, pageSize) }, blackLiveData)
    }

    //取消拉黑人的下标   成功后吧下标传过去
    val positionLiveData = MutableLiveData<Int>()
    fun cancelBlack(userId: Long, position: Int) {
        requestLoading({ userService.cancelBlack(userId) }, {
            positionLiveData.value = position
            ToastGravity.showGravity(StringUtils.getString(R.string.un_block))
        }, {
            ToastGravity.showGravity(it.errorMsg)
        })
    }

    override fun onRefresh() {
        getBlack(pageNumber = 0)
    }

    override fun onLoadMore(pageIndex: Int) {
        getBlack(pageNumber = pageIndex)
    }

}